<?php
class Banners extends Uploader
{
	const DBTable = "tbl_banners";
	const DBTable_images = "tbl_banners_images";

	public $Banner_id;
	public $Title;
	public $Is_deleted;
	public $Status;
	public $Createdate;
	public $Updatedate;

	private $__mysql;

	public function __construct()
	{
		$this->Banner_id = 0;
		$this->Title = null;
		$this->Is_deleted = 0;
		$this->Status = 0;
		$this->Createdate = date("Y-m-d H:i:s");
		$this->Updatedate = date("Y-m-d H:i:s");

		$this->__mysql = new MYSQL;
	}

	public function Save()
	{
		$sqlStr = "insert into ".self::DBTable."
				(title, status, createdate, updatedate)
				values
				(
				'".mysql_real_escape_string($this->Title)."', 
				'".$this->Status."', 
				'".$this->Createdate."', 
				'".$this->Updatedate."'
				)";
			$this->__mysql->Exec_query($sqlStr);
			return $this->__mysql->Get_insert_id();
	}

	public function Update()
	{
		$sqlStr = "update ".self::DBTable." set
			title = '".mysql_real_escape_string($this->Title)."', 
			status = '".$this->Status."', 
			updatedate = '".$this->Updatedate."'
			where banner_id = '".mysql_real_escape_string($this->Banner_id)."'";
			$this->__mysql->Exec_query($sqlStr);
	}

	public function Quick_update($column = null, $value = null)
	{
		$sqlStr = "update ".self::DBTable." set
			".$column." = '".mysql_real_escape_string($value)."',
			 updatedate = '".$this->Updatedate."' 
			where banner_id = '".mysql_real_escape_string($this->Banner_id)."'";
			$this->__mysql->Exec_query($sqlStr);
	}

	public function List_all()
	{
		$sqlStr = "select *
			from ".self::DBTable."
			where is_deleted = 0 
			order by title";
		//echo $sqlStr;
		$recordset = $this->__mysql->Exec_query($sqlStr);
		return $data_array = $this->__mysql->Get_data_array($recordset);
	}

	public function Delete($banner_id = 0)
	{
		$sqlStr = "update ".self::DBTable." set is_deleted = 1 where banner_id = '".mysql_real_escape_string($banner_id)."'";
			$this->__mysql->Exec_query($sqlStr);

		$sqlStr = "optimize table ".self::DBTable."";
			$this->__mysql->Exec_query($sqlStr);
	}

	public function Select($banner_id = 0)
	{
		$sqlStr = "select * from ".self::DBTable." where banner_id = '".mysql_real_escape_string($banner_id)."'";
		$recordset = $this->__mysql->Exec_query($sqlStr);

		if($this->__mysql->Get_num_rows($recordset) > 0)
		{
			$result = $this->__mysql->Get_data_assoc($recordset);
				$this->Banner_id = $result['banner_id'];
				$this->Title = $result['title'];
				$this->Is_deleted = $result['is_deleted'];
				$this->Status = $result['status'];
				$this->Createdate = $result['createdate'];
				$this->Updatedate = $result['updatedate'];

			return true;
		}
		else
		{
			return false;
		}
	}

	public function Select_by($fieldname = null, $value = null)
	{
		if(!is_null($fieldname))
		{
			$sqlStr = "select * from ".self::DBTable." where ".$fieldname." = '".mysql_real_escape_string($value)."'";
			$recordset = $this->__mysql->Exec_query($sqlStr);

			if($this->__mysql->Get_num_rows($recordset) > 0)
			{
				$result = $this->__mysql->Get_data_assoc($recordset);
				$this->Banner_id = $result['banner_id'];
				$this->Title = $result['title'];
				$this->Is_deleted = $result['is_deleted'];
				$this->Status = $result['status'];
				$this->Createdate = $result['createdate'];
				$this->Updatedate = $result['updatedate'];

				return true;
			}
			else
			{
				return false;
			}
		}
	}

	public function List_banner_images($banner_id = 0)
	{
		$sqlStr = "select *
			from ".self::DBTable_images."
			where is_deleted = 0 and 
			banner_id = ".$banner_id."
			order by ordering asc";
		//echo $sqlStr;
		$recordset = $this->__mysql->Exec_query($sqlStr);
		return $data_array = $this->__mysql->Get_data_array($recordset);
	}

	public function Get_active_banner_images()
	{
		$sqlStr = "select b.title as banner_title, bi.*
		from ".self::DBTable." b
		inner join ".self::DBTable_images." bi on bi.banner_id = b.banner_id
		where 
		b.status = 1 and 
		bi.is_deleted = 0
		order by bi.ordering asc
		";
		//echo $sqlStr;
		$recordset = $this->__mysql->Exec_query($sqlStr);
		return $data_array = $this->__mysql->Get_data_array($recordset);
	}
	
	public function Save_banner_image($banner_id = 0, $image = null, $url = null, $title = null, $vimeo_url = null)
	{
		$sqlStr = "insert into ".self::DBTable_images." 
			(banner_id, title, image, url, vimeo_url, createdate, updatedate)
			values
			(".$banner_id.", 
			'".mysql_real_escape_string($title)."', 
			'".mysql_real_escape_string($image)."', 
			'".mysql_real_escape_string($url)."', 
			'".mysql_real_escape_string($vimeo_url)."', 
			'".$this->Createdate."', 
			'".$this->Updatedate."')";
		//echo $sqlStr;
		$this->__mysql->Exec_query($sqlStr);
		return $this->__mysql->Get_insert_id();
	}

	public function Quick_banner_image_update($image_id = 0, $column = null, $value = null)
	{
		$sqlStr = "update ".self::DBTable_images." set
			".$column." = '".mysql_real_escape_string($value)."',
			 updatedate = '".$this->Updatedate."' 
			where image_id = '".mysql_real_escape_string($image_id)."'";
			$this->__mysql->Exec_query($sqlStr);
	}

	public function Delete_banner_image($image_id = 0)
	{
		$sqlStr = "update ".self::DBTable_images." set is_deleted = 1 where image_id = '".mysql_real_escape_string($image_id)."'";
			$this->__mysql->Exec_query($sqlStr);
	}

	public function Update_banner_ordering($image_id_array)
	{
		if(count($image_id_array) > 0)
		{
			$i = 1;
			foreach($image_id_array as $image_id)
			{
				//update the ordering of the banner that is about to be replaced
				$sqlStr = "update ".self::DBTable_images." 
					set ordering = ".$i." 
					where 
					banner_id = '".$this->Banner_id."' and 
					image_id = '".$image_id."'";
				//echo $sqlStr . "<br />";
				$this->__mysql->Exec_query($sqlStr);
				$i++;
			}
		}
	}
}
?>